//
//  RHDeviceUserCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/7/26.
//

#import "RHDeviceUserCell.h"
@interface RHDeviceUserCell()
@property(nonatomic,strong)QMUILabel *leftNameLab;
@property(nonatomic,strong)QMUILabel *rightNameLab;
@property(nonatomic,strong)UIImageView *leftIconImg;
@property(nonatomic,strong)QMUILabel *leftCountLab;
@property(nonatomic,strong)QMUILabel *rightCountLab;
@property(nonatomic,strong)UIImageView *rightIconImg;

@property(nonatomic,strong)UIButton *leftBtn;
@property(nonatomic,strong)UIButton *rightBtn;
@end
@implementation RHDeviceUserCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    
    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:UIColor.clearColor];
    CGFloat itemWidth = (kUI_WINDOW_W - 45)/2;
    
    NSArray *iconArr = @[@"data_user_icon",@"data_plant_icon"];
    NSArray *titleArr = @[@"用户".rh_localized,@"设备".rh_localized];
    
    self.leftBtn = [[UIButton alloc]init];
    kWeakSelf;
    [[self.leftBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        SEL selector = NSSelectorFromString(weakSelf.data[CellAction][0]);
        if([weakSelf.delegate respondsToSelector:selector]){
            [weakSelf.delegate performSelector:selector];
        }
    }];
    
    self.rightBtn = [[UIButton alloc]init];
    [[self.rightBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        SEL selector = NSSelectorFromString(weakSelf.data[CellAction][1]);
        if([weakSelf.delegate respondsToSelector:selector]){
            [weakSelf.delegate performSelector:selector];
        }
    }];
    
    __block UIView *lastView;
    [iconArr enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView = [self itemViewWithIndex:idx title:titleArr[idx] image:iconArr[idx]];
        [self.contentView addSubview:itemView];
        if (idx == 0) {
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(15);
                make.top.mas_offset(10);
                make.width.offset(itemWidth);
                make.height.equalTo(itemView.mas_width).multipliedBy(88./163.5);
            }];
            [itemView addSubview:weakSelf.leftBtn];
            [weakSelf.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.insets(UIEdgeInsetsZero);
            }];
        }else{
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(lastView.mas_right).offset(15);
                make.top.width.height.equalTo(lastView);
            }];
            [itemView addSubview:weakSelf.rightBtn];
            [weakSelf.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.insets(UIEdgeInsetsZero);
            }];
        }
        lastView = itemView;
    }];
    
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-10);
    }];
}

-(UIView*)itemViewWithIndex:(NSInteger)idx title:(NSString*)title image:(NSString*)imageStr
{
    
    UIView *bgView = kView;
    bgView.backgroundColor = kCOLOR_HEX(@"#F3F7FF");
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageStr]];
    [bgView addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.numberOfLines = 0;
    titleLab.text = title;
    titleLab.tag = 100 * idx;
    titleLab.textColor = [UIColor colorWithHexString:@"#171A1D" alpha:0.8];
    titleLab.font = [UIFont systemFontOfSize:16];
    [bgView addSubview:titleLab];
    
    UIImageView *arrow = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"data_arrow"]];
    [bgView addSubview:arrow];
    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab.mas_right).offset(5);
        make.centerY.equalTo(titleLab);
        make.width.height.offset(5);
    }];
    
    QMUILabel *contentLab = [[QMUILabel alloc]init];
    contentLab.textAlignment = NSTextAlignmentLeft;
    contentLab.tag = 100 * idx +1;
    contentLab.text = @"--";
    contentLab.textColor = [UIColor colorWithHexString:@"#132B88" alpha:0.6];
    contentLab.font = [UIFont boldSystemFontOfSize:20];
    [bgView addSubview:contentLab];
    
    CGFloat itemWidth = (kUI_WINDOW_W - 45)/2;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_offset(15);
        make.width.lessThanOrEqualTo(@(itemWidth-30-10));
    }];
    
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab);
        make.top.equalTo(bgView.mas_centerY).offset(0);
    }];
    
    if (idx == 0) {
        self.leftCountLab = contentLab;
    }else{
        self.rightCountLab = contentLab;
    }
    return bgView;
}

-(void)setData:(NSDictionary *)data
{
    _data = data;
  
    self.leftNameLab.text = data[CellTitle];
    self.leftCountLab.text = data[CellCount];
//    self.leftIconImg.image = [UIImage imageNamed:data[CellIcon]];
    
    self.rightNameLab.text = data[CellTitle_1];
    self.rightCountLab.text = data[CellCount_1];
//    self.rightIconImg.image = [UIImage imageNamed:data[CellIcon_1]];
}
@end
